package com.zjw.demo.vertx.starter.template.router;

import com.zjw.demo.vertx.starter.annotation.core.Inject;
import com.zjw.demo.vertx.starter.annotation.web.Get;
import com.zjw.demo.vertx.starter.annotation.web.Post;
import com.zjw.demo.vertx.starter.core.Reflector;
import com.zjw.demo.vertx.starter.plugin.impl.aop.EnableAop;
import com.zjw.demo.vertx.starter.plugin.impl.aop.Invoke;
import com.zjw.demo.vertx.starter.router.VertxRouter;
import com.zjw.demo.vertx.starter.template.service.LoginService;
import com.zjw.demo.vertx.starter.template.service.MysqlService;
import com.zjw.demo.vertx.starter.template.test.AopTest;
import com.zjw.demo.vertx.starter.utils.LogUtils;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

@VertxRouter
@EnableAop
public class LoginRouter {

  private static final String TAG = "LoginRouter";

  @Inject
  public MysqlService mysqlService;

  @Inject
  public LoginService loginService;

  @Inject
  public AopTest aopTest;

  /*
   * 登录，收到json数据{"username":"username","password":"password"}
   * */
  @Post(path = "/login")
  public void login(RoutingContext context) {
    JsonObject objects = context.body().asJsonObject();
    LogUtils.debug(TAG, "receive login message " + objects);
    String username = objects.getString("username");
    String password = objects.getString("password");

    loginService.login(username, password, context);
  }


  @Get(path = "/login/test")
  @Invoke(before = "com.zjw.demo.vertx.starter.template.test.AopLog#log")
  public void getTest(RoutingContext context) {
    //aopTest.test(context);
    context.end("my wife,please look at me,I love your,I has fly to cloud for see you~  -to zhao yang");
  }

  @Get(path = "/login/test2")
  public void helloSQL(RoutingContext context) {
//
//    DemoSqlMapping demoSqlMapping = mysqlService.optMapping(DemoSqlMapping.class);
//
//    demoSqlMapping.queryUserById(new IFutureTask<>() {
//      @Override
//      public void onResult(List<User> result) {
//        context.end(result.toString());
//      }
//
//      @Override
//      public void onError(Throwable throwable) {
//        context.end(throwable.getMessage());
//      }
//
//    }, "zjw");
  }

  @Get(path = "/login/test3")
  public void helloSQL2(RoutingContext context) {

//    DemoSqlMapping demoSqlMapping = mysqlService.optMapping(DemoSqlMapping.class);
//
//    demoSqlMapping.queryAllUser(new IFutureTask<>() {
//      @Override
//      public void onResult(List<User> result) {
//        context.end(result.toString());
//      }
//
//      @Override
//      public void onError(Throwable throwable) {
//        context.end(throwable.getMessage());
//      }
//    });
  }

  @Get(path = "/login/test4")
  public void helloSQL3(RoutingContext context) {

//    DemoSqlMapping demoSqlMapping = mysqlService.optMapping(DemoSqlMapping.class);
//
//    String username = context.queryParams().get("user");
//    String password = context.queryParams().get("pass");
//    demoSqlMapping.insertUser(new IFutureTask<>() {
//      @Override
//      public void onResult(List<User> result) {
//        context.end(result.toString());
//      }
//
//      @Override
//      public void onError(Throwable throwable) {
//        context.end(throwable.getMessage());
//      }
//
//    }, username, password);
  }
}
